package com.sdruixinggroup.mondayb2b.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sdruixinggroup.mondayb2b.R;
import com.sdruixinggroup.mondayb2b.models.Order;
import com.sdruixinggroup.mondayb2b.widget.OrderGoodView;

import java.util.List;

/**
 * Created by 00029935 on 2017/12/7.
 */

public class MyOrderAdapter extends QuickAdapter<Order.OrdersBean.DataBean> {
    private Context context;
    private OnOrderHandler onOrderHandler;

    public MyOrderAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getLayoutID(int viewType) {
        return R.layout.my_order_adapter_item;
    }

    @Override
    public void convert(VH holder, final Order.OrdersBean.DataBean itemData, final int position) {
        TextView tvOrderNO = holder.getView(R.id.tv_order_NO);
        tvOrderNO.setText("订单编号："+itemData.getOrder_sn());

        TextView tvStatus = holder.getView(R.id.tv_status);
        tvStatus.setText(itemData.getStatus_name());

        LinearLayout llGoodsCount = holder.getView(R.id.ll_goods_count);
        llGoodsCount.removeAllViews();
        List<Order.OrdersBean.DataBean.OrderGoodsBean> orderGoodsBeen =itemData.getOrder_goods();
        int count = 0;
        if(null != orderGoodsBeen && !orderGoodsBeen.isEmpty()){
            count = orderGoodsBeen.size();
        }
        for(Order.OrdersBean.DataBean.OrderGoodsBean orderGoodsBean :orderGoodsBeen ){
            OrderGoodView orderGoodView = new OrderGoodView(context);

            orderGoodView.setData(orderGoodsBean,itemData.getStatus(),itemData.getId());
            if(count==1){
                orderGoodView.setLineisVisiable(false);
            }else {
                orderGoodView.setLineisVisiable(true);
            }
            llGoodsCount.addView(orderGoodView);
        }



        TextView tvStatusClick = holder.getView(R.id.tv_status_click);
        tvStatusClick.setVisibility(View.VISIBLE);
        //状态：-2已关闭 -1已驳回 1待付款 2待审核 3待发货 4待收货 5待评价 6已完成 ,
        String  statusName ="";
        int status = itemData.getStatus();
        Log.e("HQQ","status:  "+status);

        switch (status){
            case 1:
                statusName ="立即付款";
                break;
            case -2:
                statusName ="";
                tvStatusClick.setVisibility(View.INVISIBLE);
                break;
            case 3:
                statusName ="提醒发货";
                break;
            case 4:
                statusName ="确认收货";
                break;
            case 5:
                statusName ="去评价";
                break;
            case 6:
                statusName ="删除订单";
                break;
        }
        if(TextUtils.isEmpty(statusName)){
            tvStatusClick.setVisibility(View.GONE);
        }else if(status == 5){
            tvStatusClick.setVisibility(View.GONE);
        } else {
            tvStatusClick.setText(statusName);
            tvStatusClick.setVisibility(View.VISIBLE);
        }

        tvStatusClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != onOrderHandler){
                    onOrderHandler.litener(itemData,position);
                }
            }
        });
    }

    public interface OnOrderHandler{
        void litener( Order.OrdersBean.DataBean itemData, int position);
    }

    public void setOnOrderHandler(OnOrderHandler onOrderHandler) {
        this.onOrderHandler = onOrderHandler;
    }
}
